<?php

class SetController extends Zend_Controller_Action
{

    public function init()
    {
        $core = new Model_Core();
        /* Initialize action controller here */
        $this->view->getcontroller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
        $this->view->getaction = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
        
        $ref = urlencode("http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        $this->view->ref = $ref;
        
        //未登陆 SALE返回登陆
        $login = $core->isLogin();
        if ($login){
            $this->view->member = $login;
            if ($login[ban] == 2){
                $this->_forward('isbaned', 'set');
            }
        }else{
            $this->_redirect("/member/login?ref=$ref");
        }
        $this->view->nowtitle = "管理中心";
        
        $this->view->config = $core->getConfig();
        
        $this->_helper->layout->setLayout('set');
        $this->view->headTitle("管理中心", 'PREPEND');
    }

    
    public function indexAction()
    {
        // action body
    }
    
    public function saleAction() {
        
        $page = intval($this->_request->getParam('page', 1));
        
        $app = trim($this->_request->getParam('app', 1));
        $this->view->app = $app;
        $query = trim($this->_request->getParam('query', ""));
        $this->view->query = $query;
        
        $core = new Model_Core();
        $member =  $core->isLogin();
        
        //管理员查看全部车源
        if ($this->isAdmin() == 1){
            $uid = "";
            $this->view->isadmin = '1';
        }else{
            $uid = $member[uid];
        }
        $o_uid = trim($this->_request->getParam('uid'));
        if ($this->isAdmin() == 1 && $o_uid){
            $uid = $o_uid;
        }
        
        //输出车源列表
        $sale_arr = new Model_CarSale();
        $list = $sale_arr->getListOnSet($page, $this->view->url(array('page'=>NULL))."/page/", $app, $query, $uid);
        $this->view->list = $list;
        
        $this->view->nowtitle = "我的发布";
    }
    



    public function profitAction(){
        
        
        $this->view->nowtitle = "推广中心";
    }
    
    
    public function favoritesAction() {
    	
        
        $app = trim($this->_request->getParam('app', 1));
        $this->view->app = $app;
        
        $core = new Model_Core();
        $member =  $core->isLogin();
        $uid = $member[uid];
        //输出车源列表
        $fav_arr = new Model_CommonFavorites();
        $list = $fav_arr->getFavorites($uid);
        $this->view->list = $list;
        
        
        
        $this->view->nowtitle = "我的收藏";
    }
    
    public function msgAction() {
        
        $ac = trim($this->_request->getParam('ac', 'list'));
        $page = intval($this->_request->getParam('page', 1));
        
        $core = new Model_Core();
        $member =  $core->isLogin();
        $uid = $member[uid];
        
        $msg = new Model_CommonMsg();
        $list = $msg->getMsgList($page, $this->view->url(array('page'=>NULL))."/page/", $uid);
        $this->view->list = $list;
        
        $this->view->nowtitle = "消息中心";
    }
    
    public function profileAction() {
    	
        $app = intval($this->_request->getParam('app', 1));
        $this->view->app = $app;


        $core = new Model_Core();
        $member = $core->isLogin();
        
        if ($app == 1){
            $profile = new Model_CommonMembersProfile();
            $this->view->list = $profile->getProfile($member[uid]);
            
        }
        
        if ($app == 2){
            $core = new Model_Core();
            $this->view->question = $core->getConfig();
            $memberp = new Model_CommonMembers();
            $this->view->list = $memberp->getByUid($member[uid]);
            
        }

        
        $this->view->nowtitle = "账号管理";
    }
    
    
    //是否为ADMIN 返回GROUPID
    protected function isAdmin() {
        $core = new Model_Core();
        $member =  $core->isLogin();
        
        return $member[groupid];
    }
    
    
    //用户管理    ADMIN ONLY
    public function userAction() {
    	
        if ($this->isAdmin() != 1){
            echo "<center>您无权限访问该页面！</center>";
            exit();
        }
        
        $app = intval($this->_request->getParam('app', 1));
        $this->view->app = $app;
        
        $page = intval($this->_request->getParam('page', 1));
        $query = trim($this->_request->getParam('query', ""));
        $this->view->query = $query;
        
		$members = new Model_CommonMembers();        
        $list = $members->getMemberList($page, $this->view->url(array('page'=>NULL))."/page/", $app, $query);
        $this->view->list = $list;
        
        
        $this->view->nowtitle = "用户管理";
    }
    
    
    //编辑用户
    public function edituserAction() {
        
        if ($this->isAdmin() != 1){
        	echo "<center>您无权限访问该页面！</center>";
        	exit();
        }
        
        $uid = trim($this->_request->getParam('uid'));
        $this->view->uid = $uid;
        
        $members = new Model_CommonMembers();
        $list = $members->getByUid($uid);
        $this->view->list = $list;
        
        $group = new Model_CommonGroup();
        $this->view->group = $group->getGroupList();
                
        $this->view->nowtitle = "用户管理";
    }
    
    public function isbanedAction() {
    	
        $core = new Model_Core();
        $login = $core->isLogin();
        //没有被 BAN 返回首页
        if ($login[ban] != 2){
            $this->_forward('index','set');
        }
    }
}

